package com.wangsh.forward.outer.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wangsh.forward.outer.service.IClientOuterOperService;
import com.wangsh.forward.outer.service.IClientOuterService;
import com.wangsh.forward.util.ServiceCallableUtil;
import com.wangsh.webforward.common.pojo.ApiResponse;
import com.wangsh.webforward.common.pojo.ApiResponseEnum;
import com.wangsh.webforward.common.service.BaseServiceImpl;
import com.wangsh.webforward.common.util.ConstatFinalUtil;
import com.wangsh.webforward.common.util.ExecutorServiceUtil;
import com.wangsh.webforward.common.util.PageInfoUtil;

/**
 * 请求外部的接口的实现类
 * 做为客户端请求外部的接口
 * @author TeaBig
 */
public class ClientOuterServiceImpl extends BaseServiceImpl implements IClientOuterService
{
	private IClientOuterOperService clientOuterOperService = new ClientOuterOperServiceImpl();
	
	@Override
	public ApiResponse<Object> clientTaskListService(String url,JSONObject reqJSON)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--clientTaskListService--");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 获取所有启用的任务列表 */
		/* 按照上行的信息去请求 */
		if(reqJSON == null)
		{
			reqJSON = new JSONObject();
			reqJSON.put("version", "1");
			reqJSON.put("method", "webUrlList");
			reqJSON.put("pubKey", "1");
			reqJSON.put("encrypt", "1");
			
			JSONObject dataReqJSON = new JSONObject();
			dataReqJSON.put("pageSize", "2");
			dataReqJSON.put("currentPage", "1");
			dataReqJSON.put("clientType", "0");
			reqJSON.put("data", dataReqJSON);
		}
		
		/* 请求体 */
		Map<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("json", reqJSON.toJSONString());
		/* 请求头 */
		Map<String, String> headerMap = new HashMap<String, String>();
		/* 响应体 */
		Map<String, String> responseMap = new HashMap<String, String>();
		String response = this.httpUtil.methodPost(url, headerMap, paramsMap, responseMap);
		JSONObject responseJSON = (JSONObject) JSON.parse(response);
		if(responseJSON != null && "0".equalsIgnoreCase(responseJSON.getString("code")))
		{
			/* 多条任务列表 */
			JSONObject dataResJSON = (JSONObject) responseJSON.get("data");
			JSONArray listArr = (JSONArray) dataResJSON.get("list");
			for (Iterator iterator = listArr.iterator(); iterator.hasNext();)
			{
				JSONObject webUrlJSON = (JSONObject) iterator.next();
				/* 拿到一条任务 */
				this.clientTaskOneService(webUrlJSON);
			}
			apiResponse.setDataOne(dataResJSON);
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse ;
	}

	@Override
	public ApiResponse<Object> clientTaskOneService(JSONObject webUrlJSON)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--clientTaskOneService--");
		/* 验证请求的合法性 */
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 存储多个结果,Map存储 */
		Map<String, Object> dataMapJava = new HashMap<String, Object>();
		apiResponse.setDataMapJava(dataMapJava);

		Map<String, Object> dataOneMap = new HashMap<String, Object>();
		apiResponse.setDataOne(dataOneMap);

		/* 接收参数 */
		String batchSize = webUrlJSON.get("batchSize") + "";
		String pageSize = webUrlJSON.get("pageSize") + "";
		/* 根据总次数和每页多少条进行分页 */
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		pageInfoUtil.setTotalRecord(Integer.valueOf(batchSize));
		pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		
		for(int i = 1 ; i <= pageInfoUtil.getTotalPage() ; i ++ )
		{
			pageInfoUtil.setCurrentPage(i);
			int stRecord = pageInfoUtil.getCurrRecord() + 1; 
			int edRecord = pageInfoUtil.getCurrRecord() + pageInfoUtil.getPageSize()  ; 
			if(edRecord > pageInfoUtil.getTotalRecord())
			{
				edRecord = pageInfoUtil.getTotalRecord() ; 
			}
			ConstatFinalUtil.SYS_LOGGER.info("当前页:{},起始条数:{},结束条数:{},",pageInfoUtil.getCurrentPage(), stRecord, edRecord);
			/* 多线程启动 */
			ServiceCallableUtil serviceCallableUtil = new ServiceCallableUtil();
			String operType = "requestClientMulti" ; 
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			paramsMap.put("data", webUrlJSON);
			paramsMap.put("stRecord", stRecord);
			paramsMap.put("edRecord", edRecord);
			paramsMap.put("clientOuterOperService", clientOuterOperService);
			/* 设置参数 */
			serviceCallableUtil.setOperType(operType);
			serviceCallableUtil.setParamsMap(paramsMap);
			
			/* 启动多线程 */
			ExecutorServiceUtil.submit(serviceCallableUtil);
		}
		
		/* 重置当前页 */
		pageInfoUtil.setCurrentPage(1);
		
		/* 存储分页信息 */
		dataOneMap.put("pageInfo", pageInfoUtil.toJSON());

		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		/* 提示信息 */
		return apiResponse;
	}
}
